package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.HealthRecord;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.HealthRecordService;
import com.wty.cmims.mapper.HealthRecordMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author wty
* @description 针对表【health_record】的数据库操作Service实现
* @createDate 2024-10-10 23:24:00
*/
@Service
public class HealthRecordServiceImpl extends ServiceImpl<HealthRecordMapper, HealthRecord>
    implements HealthRecordService{

    @Override
    public ResponseResult<HealthRecord> healthRecordFind(Integer userId) {
        ResponseResult<HealthRecord> responseResult = new ResponseResult<>();
        LambdaQueryWrapper<HealthRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HealthRecord::getResidentId, userId);
        HealthRecord healthRecord = this.getOne(wrapper);
        if (healthRecord != null){
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("健康信息获取成功");
            responseResult.setData(healthRecord);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("健康信息获取失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<HealthRecord> healthRecordUpdate(HealthRecord healthRecord) {
        ResponseResult<HealthRecord> responseResult = new ResponseResult<>();
        // 设置更新时间
        Date date = new Date();
        healthRecord.setUpdatedTime(date);
        boolean update = updateById(healthRecord);
        if (update){
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("健康信息更新成功");
            responseResult.setData(healthRecord);
        } else{
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("健康信息更新失败");
        }
        return responseResult;
    }
}